package com.onpositive.dataminer.rcp.views;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.Dialog;

import com.onpositive.commons.ui.dialogs.TitledDialog;
import com.onpositive.core.runtime.IConfigurationElement;
import com.onpositive.core.runtime.Platform;
import com.onpositive.dataminer.core.DataProviderConfig;
import com.onpositive.dataminer.core.DataProviders;
import com.onpositive.dataminer.core.IDataProvider;
import com.onpositive.dataminer.core.IDataProviderConfigurator;
import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.semantic.model.api.realm.Realm;
import com.onpositive.semantic.model.api.realm.RealmAccess;
import com.onpositive.semantic.model.binding.Binding;
import com.onpositive.semantic.model.ui.property.editors.structured.ListEnumeratedValueSelector;
import com.onpositive.semantic.ui.workbench.elements.XMLView;

public class DataProvidersView extends XMLView {

	public DataProvidersView() {
		super("ui/providers.dlf");
	}

	public void newProvider() {		
		IConfigurationElement[] configurationElementsFor = Platform
				.getExtensionRegistry().getConfigurationElementsFor(
						DataProviderConfig.DATAPROVIDER_EXTENSION_POINT);
		ArrayList<DataProviderConfig> r = new ArrayList<DataProviderConfig>();
		for (IConfigurationElement e : configurationElementsFor) {
			String attribute = e.getAttribute("id");
			DataProviderConfig e2 = new DataProviderConfig(attribute);
			r.add(e2);
		}
		Binding bn = new Binding("");
		bn.setRequired(true);
		bn.setName("Data provider");
		bn.setRealm(new Realm<DataProviderConfig>(r));

		ListEnumeratedValueSelector<Object> vl = new ListEnumeratedValueSelector<Object>();
		vl.setValueAsSelection(true);
		vl.setBinding(bn);
		TitledDialog dlg = new TitledDialog(bn, vl, "New Data provider",
				"Please select provider kind");
		int open = dlg.open();
		if (open == Dialog.OK) {
			DataProviderConfig value = (DataProviderConfig) bn.getValue();
			IDataProviderConfigurator configurator = value.getConfigurator();
			if (configurator.configure(value)) {
				IDataProvider createProvider = value.createProvider();
				RealmAccess.addElement(getProviders(), createProvider);
			}
		}
	}

	public IRealm<?> getProviders() {
		return new DataProviders().getRealm();
	}
	
	
}
